﻿import QtQuick 2.15
import QtQuick.Controls 2.15
import QtQuick.Layouts 1.15
import QtQuick.Window 2.15

import "." // 导入项目的根目录

Window {
    id: mainPage
    visible: true
    width: 1200
    height: 768
    title: "CalibraTech"
    color: "#f0f0f0"
    minimumWidth: 1200 // 设置最小宽度
    minimumHeight: 768 // 设置最小高度

    property var leftRectangleWidth: communcationItem.width * 0.60
    property var leftRectangleHeight: communcationItem.height * 0.08

    property var deviceRectangleWidth: netItem.width * 0.15
    property var deviceRectangleHeight: netItem.height * 0.50

    property int deviceIdIndex: 1
    property int deviceIdIndex2: 2
    property int deviceIdIndex3: 3
    property int deviceIdIndex4: 4
    property int deviceIdIndex5: 5

    property bool isConfirmed1: false  // 用于控制按钮的启用状态
    property bool isConfirmed2: false  // 用于控制按钮的启用状态
    property bool isConfirmed3: false  // 用于控制按钮的启用状态
    property bool isConfirmed4: false  // 用于控制按钮的启用状态
    property bool isConfirmed5: false  // 用于控制按钮的启用状态

    //property bool isConfirmed1: false  // 用于控制按钮的启用状态
    //property bool isConfirmed2: false  // 用于控制按钮的启用状态
    //property bool isConfirmed3: false  // 用于控制按钮的启用状态
    //property bool isConfirmed4: false  // 用于控制按钮的启用状态
    //property bool isConfirmed5: false  // 用于控制按钮的启用状态

    function selectDevice(deviceIdIndex) {
            if (deviceIdIndex === 1) {
                console.log("Selected Device 1")
                isConfirmed1 = true  // 点击确认后启用其他按钮
            }
            else if (deviceIdIndex === 2) {
                console.log("Selected Device 2")
                isConfirmed2 = true  // 点击确认后启用其他按钮
            }
            else if (deviceIdIndex === 3) {
                console.log("Selected Device 3")
                isConfirmed3 = true  // 点击确认后启用其他按钮
            }
            else if (deviceIdIndex === 4) {
                console.log("Selected Device 4")
                isConfirmed4 = true  // 点击确认后启用其他按钮
            }
            else if (deviceIdIndex === 5) {
                console.log("Selected Device 5")
                isConfirmed5 = true  // 点击确认后启用其他按钮
            }
        }


    CanPage {
        id: canPage
        visible:false
    }

    UartPage{
        id:uartPage
        visible: false
    }

    I2CPage {
        id:i2cPage
        visible: false
    }

    SpiPage {
        id:spiPage
        visible: false
    }

    EthrnetPage {
        id:ethrnetPage
        visible: false
    }

    PciePage {
        id:pciePage
        visible: false
    }

    GpioPage {
        id:gpioPage
        visible: false
    }

    CameraPage {
        id:cameraPage
        visible: false
    }

    // 设置mainPageTheme为初始页面
    Item {
        id: mainPageTheme
        width: mainPage.width
        height: mainPage.height
        anchors.fill: parent

        // 测试模块接口框
        Rectangle {
            id: communcationItem
            width: mainPage.width * 0.20
            height: mainPage.height * 0.80
            anchors.left: mainColumn.left
            anchors.leftMargin: 5
            anchors.top: topArea.top
            anchors.topMargin: 120

            color: "#ADD8E6"
            border.color: "black"
            border.width: 1

            // 标题
            Label {
                text: "通信接口"
                id: communcationLabel
                font.pixelSize: 30
                Layout.alignment: Qt.AlignLeft | Qt.AlignTop // 使用 Layout.alignment
            }

            // 按钮布局
            ColumnLayout {
                id: communcationButton
                anchors.left: communcationItem.left
                anchors.leftMargin: 15
                anchors.top: communcationLabel.bottom
                anchors.topMargin: 10
                spacing: 20

                // UART 按钮
                Rectangle {
                    width: leftRectangleWidth
                    height: leftRectangleHeight
                    color: "transparent"
                    border.color: "#0000FF"
                    border.width: 2

                    Button {
                        id: uartButton
                        text: "UART"
                        width: parent.width
                        height: parent.height
                        font.pixelSize: 20
                        enabled: isConfirmed1  // 控制按钮是否可点击
                        onClicked: {
                            console.log("显示 UART Page 页面")
                            uartPage.visible = true
                            mainPage.visible = false
                            //stackView.replace(Qt.resolvedUrl("UartPage.qml"))
                        }
                    }
                }

                // SPI 按钮
                Rectangle {
                    width: leftRectangleWidth
                    height: leftRectangleHeight
                    color: "transparent"
                    border.color: "#0000FF"
                    border.width: 2

                    Button {
                        id: spiButton
                        text: "SPI"
                        width: parent.width
                        height: parent.height
                        font.pixelSize: 20
                        enabled: isConfirmed1  // 控制按钮是否可点击
                        onClicked: {
                            //stackView.push(Qt.resolvedUrl("SpiPage.qml"))
                            console.log("显示SPIPage页面")
                            spiPage.visible = true
                            mainPage.visible = false
                        }
                    }
                }

                // I2C 按钮
                Rectangle {
                    width: leftRectangleWidth
                    height: leftRectangleHeight
                    color: "transparent"
                    border.color: "#0000FF"
                    border.width: 2

                    Button {
                        id: i2cButton
                        text: "I2C"
                        width: parent.width
                        height: parent.height
                        font.pixelSize: 20
                        enabled: isConfirmed1  // 控制按钮是否可点击
                        onClicked: {
                            console.log("显示I2C页面")
                            i2cPage.visible = true
                            mainPage.visible = false
                        }
                    }
                }

                // 以太网 按钮
                Rectangle {
                    width: leftRectangleWidth
                    height: leftRectangleHeight
                    color: "transparent"
                    border.color: "#0000FF"
                    border.width: 2

                    Button {
                        id: ethrnetButton
                        text: "以太网"
                        width: parent.width
                        height: parent.height
                        font.pixelSize: 20
                        enabled: isConfirmed1  // 控制按钮是否可点击
                        onClicked: {
                            console.log("显示ethrnetPage页面")
                            ethrnetPage.visible = true
                            mainPage.visible = false
                        }
                    }
                }

                // CAN 按钮
                Rectangle {
                    width: leftRectangleWidth
                    height: leftRectangleHeight
                    color: "transparent"
                    border.color: "#0000FF"
                    border.width: 2

                    Button {
                        id: canButton
                        text: "CAN"
                        width: parent.width
                        height: parent.height
                        font.pixelSize: 20
                        enabled: isConfirmed1  // 控制按钮是否可点击
                        onClicked: {
                           // stackView.push(Qt.resolvedUrl("CanPage.qml"))
                            console.log("显示CANPage页面")
                            canPage.visible = true
                            mainPage.visible = false
                        }
                    }
                }

                // PCIe 按钮
                Rectangle {
                    width: leftRectangleWidth
                    height: leftRectangleHeight
                    color: "transparent"
                    border.color: "#0000FF"
                    border.width: 2

                    Button {
                        id: pcieButton
                        text: "PCIe"
                        width: parent.width
                        height: parent.height
                        font.pixelSize: 20
                        enabled: isConfirmed1  // 控制按钮是否可点击
                        onClicked: {
                            // stackView.push(Qt.resolvedUrl("CanPage.qml"))
                             console.log("显示PCIePage页面")
                             pciePage.visible = true
                             mainPage.visible = false
                        }
                    }
                }

                // GPIO 按钮
                Rectangle {
                    width: leftRectangleWidth
                    height: leftRectangleHeight
                    color: "transparent"
                    border.color: "#0000FF"
                    border.width: 2

                    Button {
                        id: gpioButton
                        text: "GPIO"
                        width: parent.width
                        height: parent.height
                        font.pixelSize: 20
                        enabled: isConfirmed1  // 控制按钮是否可点击
                        onClicked: {
                            console.log("显示UARTPage页面")
                            gpioPage.visible = true
                            mainPage.visible = false
                            //stackView.push(Qt.resolvedUrl("GpioPage.qml"))
                        }
                    }
                }

                Rectangle {
                    width: leftRectangleWidth
                    height: leftRectangleHeight
                    color: "transparent"
                    border.color: "#0000FF"
                    border.width: 2

                    Button {
                        id: cameraButton
                        text: "Camera"
                        width: parent.width
                        height: parent.height
                        font.pixelSize: 20
                        enabled: isConfirmed1  // 控制按钮是否可点击
                        onClicked: {
                            console.log("显示CameraPage页面")
                            cameraPage.visible = true
                            mainPage.visible = false
                            //stackView.push(Qt.resolvedUrl("CameraPage.qml"))
                        }
                    }
                }
            }
        }

        // 设备选择框
        Rectangle {
            id: netItem
            width: mainPageTheme.width * 0.60
            height: mainPageTheme.height * 0.13
            anchors.left: communcationItem.right
            anchors.leftMargin: 30
            anchors.top: mainPageTheme.top
            anchors.topMargin: 0

            color:  "#FFA54F"
            border.color: "black"
            border.width: 1

            // 标题
            Label {
                id: netLabel
                text: "设备选择"
                font.pixelSize: 30
                Layout.alignment: Qt.AlignLeft | Qt.AlignTop // 使用 Layout.alignment
            }

            // 按钮布局
            RowLayout {
                id: netItemButton
                anchors.left: netItem.left
                anchors.leftMargin: 15
                anchors.top: netLabel.bottom
                anchors.topMargin: 10
                spacing: netItem.width * 0.05

                // 设备1 按钮
                Rectangle {
                    id: device1Rectangle
                    width: deviceRectangleWidth
                    height: deviceRectangleHeight
                    color: "transparent"
                    // border.color: "#0000FF"
                    border.color: "lightyellow"
                    border.width: 2

                    Button {
                        id: device1Button
                        text: "设备1"
                        width: parent.width
                        height: parent.height
                        font.pixelSize: 20
                        //enabled: isConfirmed1  // 控制按钮是否可点击

                        background: Rectangle {
                            color: "lightyellow"
                            radius: 5
                        }

                        onClicked: {
                            console.log("select device 1 ")
                            //ethrnetPage.visible = true
                            //mainPage.visible = false
                            deviceIdIndex = 1
                            if(isConfirmed1)
                            {
                                uartButton.enabled = true
                                spiButton.enabled  = true
                                i2cButton.enabled  = true
                                ethrnetButton.enabled = true
                                canButton.enabled  = true
                                pcieButton.enabled = true
                                gpioButton.enabled = true
                                uartButton.enabled = true
                                cameraButton.enabled = true

                                device1Button.background.color = "#90EE90"
                            }
                            else
                            {
                                uartButton.enabled = false
                                spiButton.enabled  = false
                                i2cButton.enabled  = false
                                ethrnetButton.enabled = false
                                canButton.enabled  = false
                                pcieButton.enabled = false
                                gpioButton.enabled = false
                                uartButton.enabled = false
                                cameraButton.enabled = false
                            }
                        }
                    }
                }

                // CAN 按钮
                Rectangle {
                    width: deviceRectangleWidth
                    height: deviceRectangleHeight
                    color: "transparent"
                    border.color: "#0000FF"
                    border.width: 2

                    Button {
                        id: device2Button
                        text: "设备2"
                        width: parent.width
                        height: parent.height
                        font.pixelSize: 20
                        //enabled: isConfirmed2  // 控制按钮是否可点击

                        background: Rectangle {
                            color: "lightyellow"
                            radius: 5
                        }

                        onClicked: {
                           // stackView.push(Qt.resolvedUrl("CanPage.qml"))
                            console.log("select device 2 ")
                            //canPage.visible = true
                            //mainPage.visible = false
                            deviceIdIndex = 2
                            if(isConfirmed2)
                            {
                                uartButton.enabled = true
                                spiButton.enabled  = true
                                i2cButton.enabled  = true
                                ethrnetButton.enabled = true
                                canButton.enabled  = true
                                pcieButton.enabled = true
                                gpioButton.enabled = true
                                uartButton.enabled = true
                                cameraButton.enabled = true

                                device2Button.background.color = "#90EE90"
                            }
                            else
                            {
                                uartButton.enabled = false
                                spiButton.enabled  = false
                                i2cButton.enabled  = false
                                ethrnetButton.enabled = false
                                canButton.enabled  = false
                                pcieButton.enabled = false
                                gpioButton.enabled = false
                                uartButton.enabled = false
                                cameraButton.enabled = false
                            }
                        }
                    }
                }

                Rectangle {
                    width: deviceRectangleWidth
                    height: deviceRectangleHeight
                    color: "transparent"
                    border.color: "#0000FF"
                    border.width: 2

                    Button {
                        id: device3Button
                        text: "设备3"
                        width: parent.width
                        height: parent.height
                        font.pixelSize: 20
                        //enabled: isConfirmed3  // 控制按钮是否可点击

                        background: Rectangle {
                            color: "lightyellow"
                            radius: 5
                        }

                        onClicked: {
                            console.log("select device 3 ")
                            // cameraPage.visible = true
                            // mainPage.visible = false
                            //stackView.push(Qt.resolvedUrl("CameraPage.qml"))
                            deviceIdIndex = 3
                            if(isConfirmed3)
                            {
                                uartButton.enabled = true
                                spiButton.enabled  = true
                                i2cButton.enabled  = true
                                ethrnetButton.enabled = true
                                canButton.enabled  = true
                                pcieButton.enabled = true
                                gpioButton.enabled = true
                                uartButton.enabled = true
                                cameraButton.enabled = true

                                device3Button.background.color = "#90EE90"
                            }
                            else
                            {
                                uartButton.enabled = false
                                spiButton.enabled  = false
                                i2cButton.enabled  = false
                                ethrnetButton.enabled = false
                                canButton.enabled  = false
                                pcieButton.enabled = false
                                gpioButton.enabled = false
                                uartButton.enabled = false
                                cameraButton.enabled = false
                            }
                        }
                    }
                }

                Rectangle {
                    width: deviceRectangleWidth
                    height: deviceRectangleHeight
                    color: "transparent"
                    border.color: "#0000FF"
                    border.width: 2

                    Button {
                        id: device4Button
                        text: "设备4"
                        width: parent.width
                        height: parent.height
                        font.pixelSize: 20
                        //enabled: isConfirmed4  // 控制按钮是否可点击

                        background: Rectangle {
                            color: "lightyellow"
                            radius: 5
                        }

                        onClicked: {
                            console.log("select device 4 ")
                            // cameraPage.visible = true
                            // mainPage.visible = false
                            //stackView.push(Qt.resolvedUrl("CameraPage.qml"))
                            deviceIdIndex = 4
                            if(isConfirmed4)
                            {
                                uartButton.enabled = true
                                spiButton.enabled  = true
                                i2cButton.enabled  = true
                                ethrnetButton.enabled = true
                                canButton.enabled  = true
                                pcieButton.enabled = true
                                gpioButton.enabled = true
                                uartButton.enabled = true
                                cameraButton.enabled = true

                                device4Button.background.color = "#90EE90"
                            }
                            else
                            {
                                uartButton.enabled = false
                                spiButton.enabled  = false
                                i2cButton.enabled  = false
                                ethrnetButton.enabled = false
                                canButton.enabled  = false
                                pcieButton.enabled = false
                                gpioButton.enabled = false
                                uartButton.enabled = false
                                cameraButton.enabled = false
                            }
                        }
                    }
                }

                Rectangle {
                    width: deviceRectangleWidth
                    height: deviceRectangleHeight
                    color: "transparent"
                    border.color: "#0000FF"
                    border.width: 2

                    Button {
                        id: device5Button
                        text: "设备5"
                        width: parent.width
                        height: parent.height
                        font.pixelSize: 20
                        //enabled: isConfirmed5  // 控制按钮是否可点击

                        background: Rectangle {
                            color: "lightyellow"
                            radius: 5
                        }

                        onClicked: {
                            console.log("select device 5 ")
                            // cameraPage.visible = true
                            // mainPage.visible = false
                            //stackView.push(Qt.resolvedUrl("CameraPage.qml"))
                            deviceIdIndex = 5
                            if(isConfirmed5)
                            {
                                uartButton.enabled = true
                                spiButton.enabled  = true
                                i2cButton.enabled  = true
                                ethrnetButton.enabled = true
                                canButton.enabled  = true
                                pcieButton.enabled = true
                                gpioButton.enabled = true
                                uartButton.enabled = true
                                cameraButton.enabled = true

                                device5Button.background.color = "#90EE90"
                            }
                            else
                            {
                                uartButton.enabled = false
                                spiButton.enabled  = false
                                i2cButton.enabled  = false
                                ethrnetButton.enabled = false
                                canButton.enabled  = false
                                pcieButton.enabled = false
                                gpioButton.enabled = false
                                uartButton.enabled = false
                                cameraButton.enabled = false
                            }
                        }
                    }
                }
            }
        }

        Rectangle  {
            id: infoInreface
            width: mainPageTheme.width * 0.25
            height: mainPageTheme.height * 0.30
            anchors.left: mainPageTheme.left
            anchors.leftMargin: (mainPageTheme.width - infoInreface.width) * 0.50
            anchors.top: mainPageTheme.top
            anchors.topMargin: (mainPageTheme.height - infoInreface.height) * 0.50
            color: "#ADD8E6"

            border.color: "black"
            border.width: 1

            // 标题
            Label {
                id: testButton
                text: "测试设备信息："
                font.pixelSize: 30
                Layout.alignment: Qt.AlignLeft | Qt.AlignTop // 使用 Layout.alignment
            }

            ColumnLayout {
                id: testInfo
                anchors.left: infoInreface.left
                anchors.leftMargin: 15
                anchors.top: testButton.bottom
                anchors.topMargin: 5
                spacing: 20

                Rectangle {
                    id: user
                    width: infoInreface.width * 0.9
                    height: infoInreface.height * 0.15
                    color: "#ADD8E6"

                    RowLayout {
                        spacing: 15

                        Text {
                            id: userInfo
                            width: infoInreface.width * 0.01
                            height: infoInreface.height * 0.15
                            anchors.left: pcieTop.left
                            anchors.leftMargin: infoInreface.width * 0.3
                            text: "user:"
                            font.pixelSize: 20
                        }

                        TextField {
                            id: userInfo2
                            width: infoInreface.width * 0.5
                            height: infoInreface.height * 0.15
                            placeholderText: ""
                        }
                    }
                }

                Rectangle {
                    id: host
                    width: infoInreface.width * 0.9
                    height: infoInreface.height * 0.15
                    color: "#ADD8E6"

                    RowLayout {
                        spacing: 15

                        Text {
                            id: hostInfo
                            width: infoInreface.width * 0.01
                            height: infoInreface.height * 0.15
                            anchors.left: pcieTop.left
                            anchors.leftMargin: 5
                            text: "host:"
                            font.pixelSize: 20
                        }

                        TextField {
                            id: hostInfo2
                            width: infoInreface.width * 0.5
                            height: infoInreface.height * 0.15
                            placeholderText: ""
                        }
                    }
                }

                Rectangle {
                    id: okButton
                    width: infoInreface.width * 0.9
                    height: infoInreface.height * 0.15
                    color: "#ADD8E6"

                    Button {
                        id: okButton0
                        text: "确  认"
                        width: infoInreface.width * 0.30
                        height: infoInreface.height * 0.15
                        font.pixelSize: 20
                        onClicked: {
                            backend.setHostInfo(hostInfo2.text, userInfo2.text);
                            //isConfirmed1 = true  // 点击确认后启用其他按钮
                            // 判断信息是否正确
                            selectDevice(deviceIdIndex)
                        }
                    }
                }
            }
        }
    }
}
